<template>
  <div class="inquiry-operation-log">
    <!-- 搜索和筛选 -->
    <el-card class="filter-card">
      <el-form :model="search" inline>
        <el-form-item label="用户名/用户ID">
          <el-input
            v-model="search.keyword"
            placeholder="请输入用户名或用户ID"
            prefix-icon="el-icon-search"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        
        <el-form-item label="开始日期">
          <el-date-picker
            v-model="search.startDate"
            type="date"
            placeholder="选择开始日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 160px"
          />
        </el-form-item>
        
        <el-form-item label="结束日期">
          <el-date-picker
            v-model="search.endDate"
            type="date"
            placeholder="选择结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 160px"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="fetchData">
            <i class="el-icon-refresh"></i>
            查询
          </el-button>
          <el-button @click="handleReset">
            <i class="el-icon-refresh-left"></i>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <div slot="header" class="table-header">
        <span>操作日志列表</span>
      </div>
      
      <el-table
        v-loading="loading"
        :data="logs"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column prop="UserName" label="用户名" width="120" />
        <el-table-column prop="UserId" label="用户ID" width="100" />
        <el-table-column prop="Ip" label="IP地址" width="140" />
        <el-table-column prop="OpertionContent" label="操作内容" min-width="200" />
        <el-table-column prop="CreateTime" label="操作时间" width="160" />
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="itemsPerPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItems"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { GetInquiryOpertionLogPage } from '@/api/InquiryOpertionLog'

export default {
  name: 'InquiryOpertionLog',
  data() {
    return {
      loading: false,
      // 分页
      page: 1,
      itemsPerPage: 10,
      totalItems: 0,
      // 查询条件
      search: {
        keyword: '',
        status: '',
        startDate: '',
        endDate: ''
      },
      // 日期选择器配置
      datePickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      logs: []
    }
  },
  mounted() {
    const today = this.getToday()
    this.search.startDate = today.substring(0, 7) + '-01'
    this.search.endDate = today
    this.fetchData()
  },
  methods: {
    // 获取今天 yyyy-MM-dd
    getToday() {
      const d = new Date()
      const yyyy = d.getFullYear()
      const mm = String(d.getMonth() + 1).padStart(2, '0')
      const dd = String(d.getDate()).padStart(2, '0')
      return `${yyyy}-${mm}-${dd}`
    },
    async fetchData() {
      this.loading = true
      try {
        const params = {
          pageIndex: this.page,
          pageSize: this.itemsPerPage,
          key: this.search.keyword,
          start: this.search.startDate,
          end: this.search.endDate
        }
        const res = await GetInquiryOpertionLogPage(params)
        this.logs = (res && res.Data && res.Data.Data) ? res.Data.Data : []
        this.totalItems = (res && res.Data && res.Data.Count) ? res.Data.Count : 0
      } catch (error) {
        console.error('获取操作记录失败:', error)
        this.$message.error('获取操作记录失败')
      } finally {
        this.loading = false
      }
    },
    
    // 重置搜索条件
    handleReset() {
      const today = this.getToday()
      this.search = {
        keyword: '',
        status: '',
        startDate: today.substring(0, 7) + '-01',
        endDate: today
      }
      this.page = 1
      this.fetchData()
    },
    
    // 分页大小改变
    handleSizeChange(val) {
      this.itemsPerPage = val
      this.page = 1
      this.fetchData()
    },
    
    // 当前页改变
    handleCurrentChange(val) {
      this.page = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.inquiry-operation-log {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.filter-form {
  padding: 20px;
}

.filter-form .el-form-item {
  margin-bottom: 18px;
}

.filter-actions {
  text-align: right;
  margin-top: 10px;
}

.filter-actions .el-button {
  margin-left: 10px;
}

.table-card {
  margin-bottom: 20px;
}

.table-card .el-card__body {
  padding: 0;
}

.operation-table {
  width: 100%;
}

.operation-table .el-table__header {
  background-color: #f5f7fa;
}

.operation-table .el-table th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

.operation-table .el-table td {
  padding: 12px 0;
}

.pagination-wrapper {
  text-align: right;
  padding: 20px;
  background-color: #fff;
  border-top: 1px solid #ebeef5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .inquiry-operation-log {
    padding: 10px;
  }
  
  .filter-form {
    padding: 15px;
  }
  
  .filter-form .el-form-item {
    margin-bottom: 15px;
  }
  
  .filter-actions {
    text-align: center;
  }
  
  .pagination-wrapper {
    text-align: center;
    padding: 15px;
  }
  
  .operation-table .el-table td {
    padding: 8px 0;
    font-size: 12px;
  }
}
</style>